<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>functionmock.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_functionmock.js.html">functionmock.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Enable mocking of functions not attached to objects
<a name="line17"></a> * whether they be global / top-level or anonymous methods / closures.
<a name="line18"></a> *
<a name="line19"></a> * See the unit tests for usage.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.testing&#39;);
<a name="line24"></a>goog.provide(&#39;goog.testing.FunctionMock&#39;);
<a name="line25"></a>goog.provide(&#39;goog.testing.GlobalFunctionMock&#39;);
<a name="line26"></a>goog.provide(&#39;goog.testing.MethodMock&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.LooseMock&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.Mock&#39;);
<a name="line31"></a>goog.require(&#39;goog.testing.MockInterface&#39;);
<a name="line32"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line33"></a>goog.require(&#39;goog.testing.StrictMock&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Class used to mock a function. Useful for mocking closures and anonymous
<a name="line38"></a> * callbacks etc. Creates a function object that extends goog.testing.Mock.
<a name="line39"></a> * @param {string=} opt_functionName The optional name of the function to mock.
<a name="line40"></a> *     Set to &#39;[anonymous mocked function]&#39; if not passed in.
<a name="line41"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line42"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line43"></a> * @return {goog.testing.MockInterface} The mocked function.
<a name="line44"></a> * @suppress {missingProperties} Mocks do not fit in the type system well.
<a name="line45"></a> */
<a name="line46"></a>goog.testing.FunctionMock = function(opt_functionName, opt_strictness) {
<a name="line47"></a>  var fn = function() {
<a name="line48"></a>    var args = Array.prototype.slice.call(arguments);
<a name="line49"></a>    args.splice(0, 0, opt_functionName || &#39;[anonymous mocked function]&#39;);
<a name="line50"></a>    return fn.$mockMethod.apply(fn, args);
<a name="line51"></a>  };
<a name="line52"></a>  var base = opt_strictness === goog.testing.Mock.LOOSE ?
<a name="line53"></a>      goog.testing.LooseMock : goog.testing.StrictMock;
<a name="line54"></a>  goog.object.extend(fn, new base({}));
<a name="line55"></a>
<a name="line56"></a>  return /** @type {goog.testing.MockInterface} */ (fn);
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Mocks an existing function. Creates a goog.testing.FunctionMock
<a name="line62"></a> * and registers it in the given scope with the name specified by functionName.
<a name="line63"></a> * @param {Object} scope The scope of the method to be mocked out.
<a name="line64"></a> * @param {string} functionName The name of the function we&#39;re going to mock.
<a name="line65"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line66"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line67"></a> * @return {goog.testing.MockInterface} The mocked method.
<a name="line68"></a> */
<a name="line69"></a>goog.testing.MethodMock = function(scope, functionName, opt_strictness) {
<a name="line70"></a>  if (!(functionName in scope)) {
<a name="line71"></a>    throw Error(functionName + &#39; is not a property of the given scope.&#39;);
<a name="line72"></a>  }
<a name="line73"></a>
<a name="line74"></a>  var fn = goog.testing.FunctionMock(functionName, opt_strictness);
<a name="line75"></a>
<a name="line76"></a>  fn.$propertyReplacer_ = new goog.testing.PropertyReplacer();
<a name="line77"></a>  fn.$propertyReplacer_.set(scope, functionName, fn);
<a name="line78"></a>  fn.$tearDown = goog.testing.MethodMock.$tearDown;
<a name="line79"></a>
<a name="line80"></a>  return fn;
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Resets the global function that we mocked back to its original state.
<a name="line86"></a> * @this {goog.testing.MockInterface}
<a name="line87"></a> */
<a name="line88"></a>goog.testing.MethodMock.$tearDown = function() {
<a name="line89"></a>  this.$propertyReplacer_.reset();
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Mocks a global / top-level function. Creates a goog.testing.MethodMock
<a name="line95"></a> * in the global scope with the name specified by functionName.
<a name="line96"></a> * @param {string} functionName The name of the function we&#39;re going to mock.
<a name="line97"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line98"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line99"></a> * @return {goog.testing.MockInterface} The mocked global function.
<a name="line100"></a> */
<a name="line101"></a>goog.testing.GlobalFunctionMock = function(functionName, opt_strictness) {
<a name="line102"></a>  return goog.testing.MethodMock(goog.global, functionName, opt_strictness);
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Convenience method for creating a mock for a function.
<a name="line108"></a> * @param {string=} opt_functionName The optional name of the function to mock
<a name="line109"></a> *     set to &#39;[anonymous mocked function]&#39; if not passed in.
<a name="line110"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line111"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line112"></a> * @return {goog.testing.MockInterface} The mocked function.
<a name="line113"></a> */
<a name="line114"></a>goog.testing.createFunctionMock = function(opt_functionName, opt_strictness) {
<a name="line115"></a>  return goog.testing.FunctionMock(opt_functionName, opt_strictness);
<a name="line116"></a>};
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * Convenience method for creating a mock for a method.
<a name="line121"></a> * @param {Object} scope The scope of the method to be mocked out.
<a name="line122"></a> * @param {string} functionName The name of the function we&#39;re going to mock.
<a name="line123"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line124"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line125"></a> * @return {goog.testing.MockInterface} The mocked global function.
<a name="line126"></a> */
<a name="line127"></a>goog.testing.createMethodMock = function(scope, functionName, opt_strictness) {
<a name="line128"></a>  return goog.testing.MethodMock(scope, functionName, opt_strictness);
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Convenience method for creating a mock for a constructor. Copies class
<a name="line134"></a> * members to the mock.
<a name="line135"></a> *
<a name="line136"></a> * &lt;p&gt;When mocking a constructor to return a mocked instance, remember to create
<a name="line137"></a> * the instance mock before mocking the constructor. If you mock the constructor
<a name="line138"></a> * first, then the mock framework will be unable to examine the prototype chain
<a name="line139"></a> * when creating the mock instance.
<a name="line140"></a> * @param {Object} scope The scope of the constructor to be mocked out.
<a name="line141"></a> * @param {string} constructorName The name of the constructor we&#39;re going to
<a name="line142"></a> *     mock.
<a name="line143"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line144"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line145"></a> * @return {goog.testing.MockInterface} The mocked constructor.
<a name="line146"></a> */
<a name="line147"></a>goog.testing.createConstructorMock = function(scope, constructorName,
<a name="line148"></a>                                              opt_strictness) {
<a name="line149"></a>  var realConstructor = scope[constructorName];
<a name="line150"></a>  var constructorMock = goog.testing.MethodMock(scope, constructorName,
<a name="line151"></a>                                                opt_strictness);
<a name="line152"></a>
<a name="line153"></a>  // Copy class members from the real constructor to the mock. Do not copy
<a name="line154"></a>  // the closure superClass_ property (see goog.inherits), the built-in
<a name="line155"></a>  // prototype property, or properties added to Function.prototype
<a name="line156"></a>  // (see goog.MODIFY_FUNCTION_PROTOTYPES in closure/base.js).
<a name="line157"></a>  for (var property in realConstructor) {
<a name="line158"></a>    if (property != &#39;superClass_&#39; &amp;&amp;
<a name="line159"></a>        property != &#39;prototype&#39; &amp;&amp;
<a name="line160"></a>        realConstructor.hasOwnProperty(property)) {
<a name="line161"></a>      constructorMock[property] = realConstructor[property];
<a name="line162"></a>    }
<a name="line163"></a>  }
<a name="line164"></a>  return constructorMock;
<a name="line165"></a>};
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>/**
<a name="line169"></a> * Convenience method for creating a mocks for a global / top-level function.
<a name="line170"></a> * @param {string} functionName The name of the function we&#39;re going to mock.
<a name="line171"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line172"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line173"></a> * @return {goog.testing.MockInterface} The mocked global function.
<a name="line174"></a> */
<a name="line175"></a>goog.testing.createGlobalFunctionMock = function(functionName, opt_strictness) {
<a name="line176"></a>  return goog.testing.GlobalFunctionMock(functionName, opt_strictness);
<a name="line177"></a>};
</pre>


</body>
</html>
